import {Component, Input, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {NbMenuService, NbSidebarService} from '@nebular/theme';
import {AnalyticsService} from '../../../@core/utils/analytics.service';
import {NbAuthJWTToken, NbAuthService} from '@nebular/auth';
import {UserModel} from '../../../models/user.model'
import {ShareService} from '../../../service/share.service';

@Component({
  selector: 'ngx-header',
  styleUrls: ['./header.component.scss'],
  templateUrl: './header.component.html',
})
export class HeaderComponent implements OnInit {


  @Input() position = 'normal';

  user: UserModel = new UserModel();

  userMenu = [
    {title: 'Profile', link: 'users/user-profile', icon: 'nb-compose'},
    {
      title: 'Log out',
      link: '/auth/login',
      icon: 'nb-close',
    }];

  constructor(private sidebarService: NbSidebarService,
              private menuService: NbMenuService,
              private analyticsService: AnalyticsService,
              private shareService: ShareService,
              private authService: NbAuthService, private router: Router) {
  }

  ngOnInit() {
    this.authService.onTokenChange().subscribe((token: NbAuthJWTToken) => {
      if (token.isValid()) {
        const payLoad = token.getPayload();
        this.user.userName = payLoad.user_name;
        this.user.isLogin = true;
        this.user.id = payLoad.user_id;
        this.shareService.updateData(this.user);
      }
    });
  }

  toggleSidebar(): boolean {
    this.sidebarService.toggle(true, 'menu-sidebar');
    return false;
  }

  toggleSettings(): boolean {
    this.sidebarService.toggle(false, 'settings-sidebar');
    return false;
  }

  goToHome() {
    this.menuService.navigateHome();
  }

  signIn(): void {
    this.router.navigate(['auth/login']);
  }

  startSearch() {
    this.analyticsService.trackEvent('startSearch');
  }
}
